<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sanpham_model extends CI_Model {

	var $masp='';
	var $tensanpham='';
	var $ma_dm='';
	var $mota='';
	var $dongia='';
	var $soluong='';
	var $chitiet='';
	var $hinh='';
	var $donvitinh='';
	var $ngaynhap='';
	var $soluotxem='';
	var $soluotdathang='';
	var $mota_en='';
	var $chitiet_en='';
	
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->load->database();
    }
	 
	function insert($data)
    {
        $this->db->insert('sanpham', $data);
		$result=$this->db->query("SELECT LAST_INSERT_ID();");
		$db=$result->row_array();
		return $db['LAST_INSERT_ID()'];
    }
	function xoahinhsanpham($masp)
	{
		$row=$this->db->query("SELECT `hinh` FROM `sanpham` WHERE `masp`='".$masp."'");
		$db1=$row->row_array();
		if(!empty($db1))
		{
			$myFile = $db1['hinh'];
			$myFile=str_replace("/", "\\", $myFile);
			$unlinkdir = getcwd().'\\'.$myFile; 
			unlink($unlinkdir);
			
		}
	}
	
	
	function delete($masp)
    {
		$this->xoahinhsanpham($masp);
			
		$result=$this->db->query("SELECT `tenhinh` FROM `hinh` WHERE `ma_sp`='".$masp."'");
		$db2=$result->result_array();
		if(!empty($db2))
		{
			foreach ($db2 as $h)
			{
				if(!empty($h))
				{
					$myFile = $h['tenhinh'];
					$myFile=str_replace("/", "\\", $myFile);
					$unlinkdir = getcwd().'\\'.$myFile; 
					unlink($unlinkdir);
					
				}
			}
			
		}
		
		$this->db->delete('hinh', "`ma_sp` = '".$masp."'");
		$this->db->delete('ddh_sp', "`ma_sp` = '".$masp."'");
        $this->db->delete('sanpham', "`masp` = '".$masp."'");
	
    }
	function getsanpham($masp)
	{
		$this->db->select("masp,tensanpham,ma_dm,mota,dongia,soluong,chitiet,hinh,donvitinh,ngaynhap,soluotxem,soluotdathang,mota_en,chitiet_en");
		$this->db->where("masp",$masp);
        $query=$this->db->get("sanpham");
       	return $query->row_array();
	}
	function insert_hinh($data)
    {
      	return $this->db->insert('hinh', $data);
    }
	function getresult_hinh($masp)
    {
      	$this->db->select("mah,tenhinh");
		$this->db->where("ma_sp",$masp);
        $query=$this->db->get("hinh");
       	return $query->result_array();
    }
	function update($data,$masp)
    {
		$this->db->where("masp",$masp);
      	return $this->db->update('sanpham', $data);
    }
	function getresult_index()
	{
		$data= $this->input->get();
		$index=1;
		$baitren_mottrang=2;
		
		if(!empty($data['page']))
		{
			$index=$data['page'];
		}else $data['page']=1;
		$query1=null;
		$query2=null;
		if(!empty($data['dm'])&&!empty($data['key']))
		{	
			$query1=$this->db->query("SELECT `masp` FROM `sanpham` WHERE `ma_dm`='".$data['dm']."' and  `tensanpham` like '%".$data['key']."%'") ;
			$query2=$this->db->query("SELECT `masp`,`tensanpham`,`soluong`,`dongia`,`donvitinh`,`soluotxem`,`soluotdathang` FROM `sanpham`  WHERE (`ma_dm`='".$data['dm']."' AND `tensanpham` like '%".$data['key']."%') ORDER BY `masp` DESC  LIMIT ".(($index-1)*$baitren_mottrang).",".$baitren_mottrang) ;
		}else if(!empty($data['key']))
		{
			$query1=$this->db->query("SELECT `masp` FROM `sanpham` WHERE `masp` like '%".$data['key']."%' ") ;
			$query2=$this->db->query("SELECT `masp`,`tensanpham`,`soluong`,`dongia`,`donvitinh`,`soluotxem`,`soluotdathang`  FROM `sanpham` WHERE `tensanpham` like '%".$data['key']."%'  ORDER BY `masp` DESC  LIMIT ".(($index-1)*$baitren_mottrang).",".$baitren_mottrang) ;
		}else if(!empty($data['dm']))
		{
			$query1=$this->db->query("SELECT `masp` FROM `sanpham` WHERE `ma_dm`='".$data['dm']."' ") ;
			$query2=$this->db->query("SELECT `masp`,`tensanpham`,`soluong`,`dongia`,`donvitinh`,`soluotxem`,`soluotdathang` FROM `sanpham`  WHERE (`ma_dm`='".$data['dm']."') ORDER BY `masp` DESC  LIMIT ".(($index-1)*$baitren_mottrang).",".$baitren_mottrang) ;
		}else 
		{
			$query1=$this->db->query("SELECT `masp` FROM `sanpham`  ") ;
			$query2=$this->db->query("SELECT `masp`,`tensanpham`,`soluong`,`dongia`,`donvitinh`,`soluotxem`,`soluotdathang` FROM `sanpham`  ORDER BY `masp` DESC  LIMIT ".(($index-1)*$baitren_mottrang).",".$baitren_mottrang) ;
		}	
		
		$db1=$query1->num_rows();
		$db2=$query2->result_array();
        
		$data['pages']=$db1/$baitren_mottrang;
		$data['sanphams']=$db2;
		$data['pagesize']=$baitren_mottrang;
       	return $data;
	 }
	 
	 
	 
	 
	 
}

?>